#include "cryptiondialog.h"
#include "ui_cryptiondialog.h"
#include "fileio.h"

#include<QtGui>

CryptionDialog::CryptionDialog(QWidget *parent) :
    QTabWidget(parent),
    ui(new Ui::CryptionDialog)
{
    ui->setupUi(this);
//    des=new DES();
    //Encryption Window
    connect(ui->pushButton_2,SIGNAL(clicked(bool)),this,SLOT(setTextPath()));
    connect(ui->pushButton_3,SIGNAL(clicked(bool)),this,SLOT(setKeyPath()));
    connect(ui->pushButton_4,SIGNAL(clicked(bool)),this,SLOT(SaveText()));
    connect(ui->lineEdit_1, SIGNAL(textChanged(const QString &)), this, SLOT(ready_encrypt()));
    connect(ui->lineEdit_2, SIGNAL(textChanged(const QString &)), this, SLOT(ready_encrypt()));
    connect(ui->lineEdit_3, SIGNAL(textChanged(const QString &)), this, SLOT(ready_encrypt()));

    //Decryption Window
    connect(ui->pushButton_6,SIGNAL(clicked(bool)),this,SLOT(setTextPath2()));
    connect(ui->pushButton_7,SIGNAL(clicked(bool)),this,SLOT(setKeyPath2()));
    connect(ui->pushButton_8,SIGNAL(clicked(bool)),this,SLOT(SaveText2()));
    connect(ui->lineEdit_7, SIGNAL(textChanged(const QString &)), this, SLOT(ready_decrypt()));
    connect(ui->lineEdit_8, SIGNAL(textChanged(const QString &)), this, SLOT(ready_decrypt()));
    connect(ui->lineEdit_9, SIGNAL(textChanged(const QString &)), this, SLOT(ready_decrypt()));

    QObject::connect(ui->pushButton_5, SIGNAL(clicked(bool)), this, SLOT(encryptSlot()));
    QObject::connect(ui->pushButton_9, SIGNAL(clicked(bool)), this, SLOT(decryptSlot()));
}


CryptionDialog::~CryptionDialog()
{
    delete ui;
//    delete des;
}

void CryptionDialog::setTextPath()
{
    QString fileName=QFileDialog::getOpenFileName(this,tr("Choose File"),".",tr("All Files (*.txt)"));
    ui->lineEdit_1->setText(fileName);
}

QString CryptionDialog::getTextPath()
{
    QString fileName=ui->lineEdit_1->text();
    return fileName;
}
void CryptionDialog::setTextPath2()
{
    QString fileName=QFileDialog::getOpenFileName(this,tr("Choose File"),".",tr("All Files (*.txt)"));
    ui->lineEdit_7->setText(fileName);
}
void CryptionDialog::setKeyPath()
{
    QString fileName=QFileDialog::getOpenFileName(this,tr("Choose File"),".",tr("All Files (*.txt)"));
    ui->lineEdit_2->setText(fileName);
}
QString CryptionDialog::getKeyPath()
{
    QString fileName=ui->lineEdit_2->text();
    return fileName;
}

void CryptionDialog::setKeyPath2()
{
    QString fileName=QFileDialog::getOpenFileName(this,tr("Choose File"),".",tr("All Files (*.txt)"));
    ui->lineEdit_8->setText(fileName);
}

void CryptionDialog::SaveText()
{
    QString fileName=QFileDialog::getSaveFileName(this,tr("Save File"),".",tr("All Files (*.txt)"));
    ui->lineEdit_3->setText(fileName);
}

void CryptionDialog::SaveText2()
{
    QString fileName=QFileDialog::getSaveFileName(this,tr("Save File"),".",tr("All Files (*.txt)"));
    ui->lineEdit_9->setText(fileName);
}

void CryptionDialog::ready_encrypt()
{
    QFileInfo Plain_name(ui->lineEdit_1->text());
    QFileInfo Key_name(ui->lineEdit_2->text());
    QString Cipher_name(ui->lineEdit_3->text());
    if(Plain_name.isFile() && Key_name.isFile()&& !Cipher_name.isEmpty() )
        ui->pushButton_5->setEnabled(true);
}

void CryptionDialog::ready_decrypt()
{
    QFileInfo Cipher_name(ui->lineEdit_7->text());
    QFileInfo Key_name(ui->lineEdit_8->text());
    QString Plain_name(ui->lineEdit_9->text());
    if(Cipher_name.isFile() && Key_name.isFile()&& !Plain_name.isEmpty() )
        ui->pushButton_9->setEnabled(true);
}

void CryptionDialog::encryptSlot()
{
    ui->pushButton_5->setDisabled(true);
    QString Plain_name=ui->lineEdit_1->text();
    QString Cipher_name=ui->lineEdit_3->text();
    QString Key_name=ui->lineEdit_2->text();


    //
    vector <unsigned char> text;
    QString plaintext="ptext.txt";
    QString keytext="ktext.txt";
    QString ciphertext="ctext.txt";
    FileIO fileio;
    fileio.readPCText(Plain_name,0,text);
    string pstr;
    for(int i=0;i<text.size();++i)
        pstr+=text[i];
    des.saveData(plaintext.toStdString(),pstr);

    pstr.clear();
    fileio.readPCText(Key_name,0,text);
    for(int i=0;i<text.size();++i)
        pstr+=text[i];
    des.saveData(keytext.toStdString(),pstr);

    des.setfilename(plaintext,ciphertext,keytext);

//    des->setfilename(ui->lineEdit_1->text(),ui->lineEdit_3->text(),ui->lineEdit_2->text());
    des.encrypt(true);
    pstr=des.readData(ciphertext.toStdString());
//    fileio.readPCText(ciphertext,0,text);
    text.clear();
    for(int i=0;i<pstr.length();++i)
    {
        text.push_back(pstr[i]);
    }
    fileio.writePCText(Cipher_name,0,8,text);


    //
//    QMessageBox::information(this,tr("Information"),tr("Encryption Completed!"));
    ui->pushButton_5->setEnabled(true);

}


void CryptionDialog::decryptSlot()
{
    ui->pushButton_9->setDisabled(true);
    QString Plain_name=ui->lineEdit_9->text();
    QString Cipher_name=ui->lineEdit_7->text();
    QString Key_name=ui->lineEdit_8->text();         //get filePath
  //des.setfilename(Plain_name,Cipher_name,Key_name);

    vector <unsigned char> text;
    QString plaintext="ptext.txt";
    QString keytext="ktext.txt";
    QString ciphertext="ctext.txt";

    FileIO fileio;
    string pstr;

    fileio.readPCText(Cipher_name,0,text);  ///// read Cipher Text
    for(int i=0;i<text.size();++i)
      pstr+=text[i];
    des.saveData(ciphertext.toStdString(),pstr);

      pstr.clear();

    fileio.readPCText(Key_name,0,text);
    for(int i=0;i<text.size();++i)
      pstr+=text[i];
    des.saveData(keytext.toStdString(),pstr);

    des.setfilename(plaintext,ciphertext,keytext);

//    des->setfilename(ui->lineEdit_9->text(),ui->lineEdit_7->text(),ui->lineEdit_8->text());
    des.decrypt();
     pstr=des.readData(plaintext.toStdString());
    text.clear();
    for(int i=0;i<pstr.length();++i)
       text.push_back(pstr[i]);

    fileio.writePCText(Plain_name,0,8,text);

//    QMessageBox::information(this,tr("Information"),tr("Decryption Completed!"));
    ui->pushButton_9->setEnabled(true);

}
